package com.acoreful.ackitcloud.api.holder;

import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * TokenHolder
 */
@Component
public class TokenHolder {
    private final static String TOKEN="token";
    /**
     * 增加Token
     * @param token
     */
    public void putToken(String token){
        RequestAttributes reqAttrs= RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request=((ServletRequestAttributes)reqAttrs).getRequest();
        request.getSession().setAttribute(TOKEN,token);
    }

    /**
     *  获取Token
     * @return
     */
    public String getToken(){
        RequestAttributes reqAttrs= RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request=((ServletRequestAttributes)reqAttrs).getRequest();
        Object token=request.getSession().getAttribute(TOKEN);
        return StrUtil.toStringOrNull(token);
    }
}
